package com.cloud.wms.ui.inside.transfer;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.wms.R;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.base.BaseBarActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.config.SpKey;
import com.cloud.wms.ui.inside.contract.TransferDownContact;
import com.cloud.wms.ui.inside.contract.TransferUpContact;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.inside.presenter.TransferUpPresenter;
import com.cloud.wms.ui.inside.presenter.TransferUpPresenter;
import com.cloud.wms.ui.sort.entity.DetachBoxBean;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by FC on 2021/5/18.
 * Desc：移库下架确认
 */
public class TransferUpConfirmActivity extends BaseBarActivity<TransferUpPresenter> implements TransferUpContact.View {
    @BindView(R.id.tv_bar_code)
    TextView tvBarCode;
    @BindView(R.id.tv_commodity_name)
    TextView tvCommodityName;
    @BindView(R.id.tv_pos_code)
    TextView tvPosCode;
    @BindView(R.id.tv_production_date)
    TextView tvProductionDate;
    @BindView(R.id.tv_expire_date)
    TextView tvExpireDate;
    @BindView(R.id.tv_in_date)
    TextView tvInDate;
    @BindView(R.id.tv_stock_type)
    TextView tvStockType;
    @BindView(R.id.tv_all_amount)
    TextView tvAllAmount;
    @BindView(R.id.et_amount)
    EditText etAmount;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    @BindView(R.id.tv_pos_title)
    TextView tvPosTitle;
    @BindView(R.id.tv_amount_title)
    TextView tvAmountTitle;
    @BindView(R.id.tv_et_amount_title)
    TextView tvEtAmountTitle;
    @BindView(R.id.tv_container_title)
    TextView tvContainerTitle;
    @BindView(R.id.tv_container_code)
    TextView tvContainerCode;
    TransferCommodityEntity commodityEntity;
    private String upPosCode, posID;

    @Override
    protected String barTitle() {
        return "移库上架";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_transfer_down_confirm;
    }

    @Override
    protected void initEvent() {
        commodityEntity = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);
        upPosCode = getIntent().getStringExtra(IntentKey.ID_KEY);
        posID = getIntent().getStringExtra(IntentKey.INFO_KEY);
        tvBarCode.setText(commodityEntity.getBarCode());
        tvCommodityName.setText(commodityEntity.getCommodityName());
        tvPosCode.setText(upPosCode);
        tvProductionDate.setText(commodityEntity.getProductionDate());
        tvExpireDate.setText(commodityEntity.getExpirationDate());
        tvInDate.setText(commodityEntity.getInDate());
        tvStockType.setText(commodityEntity.getStockType() == 1 ? "良品" : "次品");//1良品 2次品 3废品
        tvAllAmount.setText(String.valueOf(commodityEntity.getAmount()));
        etAmount.requestFocus();
        tvPosTitle.setText("上架库位");
        tvAmountTitle.setText("可上架数");
        tvEtAmountTitle.setText("上架数量");
        btnConfirm.setText("确认上架");
    }

    @Override
    public void onSuccess(Message message) {
        if (message.what == TransferUpPresenter.CONFIRM_UP) {
            success("上架成功");
            EventBusUtil.sendEvent(new Event<>(EventConfig.CLOSE_REFRESH));
            finish();
        }
    }

    @OnClick(R.id.btn_confirm)
    public void onClick() {
        if (TextUtils.isEmpty(etAmount.getText().toString())) {
            info("上架数量不能为空");
            return;
        }
        if (commodityEntity.getAmount() < Integer.parseInt(etAmount.getText().toString())) {
            info("上架数量不能大于可上架数");
            return;
        }
        mPresenter.confirmUp(posID, etAmount.getText().toString().trim(), commodityEntity);
    }

    public static void jumpToNext(Activity activity, TransferCommodityEntity commodityEntity, String upPosCode, String posID) {
        Intent intent = new Intent(activity, TransferUpConfirmActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, commodityEntity);
        intent.putExtra(IntentKey.ID_KEY, upPosCode);
        intent.putExtra(IntentKey.INFO_KEY, posID);
        activity.startActivity(intent);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
